package com.yongcheon.sms_manager.ui.preference;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.database.sqlite.SQLiteDatabase;
import android.view.View;

import com.yongcheon.sms_manager.R;
import com.yongcheon.sms_manager.data.preference.SpamSentenceData;
import com.yongcheon.sms_manager.database.SpamDBManager;
import com.yongcheon.sms_manager.ui.dialogs.SpamSenetnceRegistDlg;
import com.yongcheon.sms_manager.vo.SpamSentenceVO;

/**
 * Created by yongcheon on 2014-12-10.
 */
public class SpamSentenceLongClickListener implements View.OnLongClickListener {
    View view;
    SpamSentenceVO spamSentenceVO;
    SpamSentenceCursorAdapter spamSentenceCursorAdapter;

    public SpamSentenceLongClickListener(View view, SpamSentenceVO spamSentenceVO, SpamSentenceCursorAdapter spamSentenceCursorAdapter){
        super();
        this.view = view;
        this.spamSentenceVO = spamSentenceVO;
        this.spamSentenceCursorAdapter = spamSentenceCursorAdapter;
    }

    @Override
    public boolean onLongClick(View v) {
        String[] MENU_LIST = {v.getContext().getResources().getString(R.string.edit), v.getContext().getResources().getString(R.string.delete)};
        AlertDialog dialog = new AlertDialog.Builder(v.getContext())
                .setTitle(spamSentenceVO.getSentence())
                .setItems(MENU_LIST, new SpamDialogOnClickListener(v.getContext(), v, spamSentenceVO))
                .create();

        dialog.setCanceledOnTouchOutside(true);
        dialog.show();
        return false;
    }

    public class SpamDialogOnClickListener implements  DialogInterface.OnClickListener {
        private Context context;
        private View v;
        private SpamSentenceVO spamSentenceVO;

        public SpamDialogOnClickListener(Context context, View v, SpamSentenceVO spamSentenceVO) {
            super();
            this.context = context;
            this.v = v;
            this.spamSentenceVO = spamSentenceVO;
        }

        @Override
        public void onClick(DialogInterface dialog, int which) {
            switch (which) {
                case 0 :
                    SpamSenetnceRegistDlg dlg = new SpamSenetnceRegistDlg();
                    dlg.showRegSentenceDlg(context, v, spamSentenceVO, this);
                    break;
                case 1 :
                    deleteSentence(spamSentenceVO);
                    break;
            }
        }

        public void updateSentence(SpamSentenceVO spamSentenceVO){
            SpamSentenceData spamSentenceData = new SpamSentenceData(context);
            SQLiteDatabase database = SpamDBManager.getDatabase(context);
            spamSentenceData.update(spamSentenceVO, context, database);
            SpamSentenceLongClickListener.this.spamSentenceCursorAdapter.getCursor().requery();
        }

        public void deleteSentence(SpamSentenceVO spamSentenceVO){
            SpamSentenceData spamSentenceData = new SpamSentenceData(context);
            SQLiteDatabase database = SpamDBManager.getDatabase(context);
            spamSentenceData.delete(spamSentenceVO, context, database);
            SpamSentenceLongClickListener.this.spamSentenceCursorAdapter.getCursor().requery();
        }

    }
}
